package com.lanchetech.admin.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.JobService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.JobApplyVo;
import com.lanchetech.common.enums.DeletedEnum;
import com.lanchetech.common.enums.JobStatusEnum;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.dao.JobMapper;
import com.lanchetech.entity.Job;
import com.lanchetech.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class JobServiceImpl implements JobService {

    @Autowired
    JobMapper jobMapper;

    @Override
    public BaseResp editJob(Job job) {
        if(job.getId() == null){
            //添加职位
            job.setCreatedAt(new Date());
            job.setUpdatedAt(new Date());
            job.setDeleted(DeletedEnum.DEFAULT.getStatus());
            jobMapper.insert(job);
        }else {
            //修改职位
            job.setUpdatedAt(new Date());
            jobMapper.updateByPrimaryKeySelective(job);
        }
        return new BaseResp();
    }

    @Override
    public ResultData<Job> getJobDetail(Long id) {
        Job job = jobMapper.selectByPrimaryKey(id);
        return new ResultData<>(job);
    }

    @Override
    public BaseResp deleteJob(Long id) {
        Job job = jobMapper.selectByPrimaryKey(id);
        if(job.getDeleted().equals(DeletedEnum.DEFAULT.getStatus())){
            job.setUpdatedAt(new Date());
            job.setDeleted(DeletedEnum.DELETED.getStatus());
            jobMapper.updateByPrimaryKeySelective(job);
            return new BaseResp();
        }
        return new BaseResp(ResultCode.FAIL);
    }

    @Override
    public ResultData<BasePageResp<Job>> getJobPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<Job> list = jobMapper.findAllPage(req);
        return PageHelpUtil.buildPage(list,page);
    }


}
